/************************************************************************************
* Y.cpp                                                                             *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/


#include "Y.h"
#include "SettingConstants.h"

#include <QSqlDatabase>
#include <QVariant>
#include <QDebug>
#include <QSqlError>
#include <QFont>
#include <QColor>
#include <QSize>
#include <QImage>



Y* Y::__instance = new Y();

Y::Y() {
}


Y* Y::get() {
    if(__instance == NULL) {
        __instance = new Y();
        __instance->init();
    }
    return __instance;
}


void Y::init() {
//    m_confMap.insert(CoreCfg::LFPR_COLOR_ALL_TEXT, QColor(Qt::green));
//    m_confMap.insert(CoreCfg::LFPR_FONT_SCROLL, QFont("Droid Sans"));
//    m_confMap.insert(CoreCfg::LFPR_FONT_TIME, QFont("Monospace", 11, 3, false));
//    m_confMap.insert(CoreCfg::LFPR_FONT_INFO, QFont("Monospace", 8));
//    m_confMap.insert(CoreCfg::LFPR_IMAGE_BACKGROUND, QImage(":/player"));

//    //GLOBAL
//    m_confMap.insert(CoreCfg::LFGL_FONTGLOBAL, QFont("Droid Sans"));


//    //PLAYLIST
//    m_confMap.insert(CoreCfg::LFPL_ICON_ADD_FOLDER, QIcon(":/add_folder"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_ADD_FILES, QIcon(":/add_files"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_MOVE_UP, QIcon(":/move_up"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_MOVE_DOWN, QIcon(":/move_down"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_REMOVE_TRACK, QIcon(":/remove_selected"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_CLEAR, QIcon(":/clear_playlist"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_SHUFFLE_OFF, QIcon(":/sequential"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_SHUFFLE_ON, QIcon(":/shuffle"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_REPEAT_OFF, QIcon(":/no_repeat"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_REPEAT_ALL, QIcon(":/repeat_all"));
//    m_confMap.insert(CoreCfg::LFPL_ICON_REPEAT_ONE, QIcon(":/repeat_one"));

//    //Collector
//    m_confMap.insert(CoreCfg::LFCL_ICON_RATE_STAR, QImage(":/rating"));
//    m_confMap.insert(CoreCfg::LFCL_ICON_RATE_NOSTAR, QImage(":/rating_disabled"));
//    m_confMap.insert(CoreCfg::LFCL_ICON_ADD_TO_CURRENT_PL, "");
//    m_confMap.insert(CoreCfg::LFCL_ICON_REMOVE, "");
//    m_confMap.insert(CoreCfg::LFCL_ICON_CLEAR, "");
//    m_confMap.insert(CoreCfg::LFCL_ICON_ADD_TO_PLAYLIST, "");
}

void Y::addConfig(QString key, QVariant* value) {
    if(value != NULL) {
        m_confMap.insert(key, value);
    }
}


void Y::addConfig(QString key, QString value) {
    m_confMap.insert(key, new QVariant(value));
}


void Y::addConfig(QString key, int value) {
    m_confMap.insert(key, new QVariant(value));
}



void Y::addConfig(QString key, double value) {
    m_confMap.insert(key, new QVariant(value));
}


void Y::removeConfig(QString key) {
    if(m_confMap.contains(key)) {
        delete m_confMap.value(key);
        m_confMap.remove(key);
    }
}


bool Y::has(QString key) {
    return m_confMap.contains(key);
}


QVariant* Y::config(QString key, QVariant *fallback) {
    if(m_confMap.contains(key)) {
         return m_confMap.value(key);
    }
    return fallback;
}


QString Y::string(QString key, QString &fallback) {
    if (m_confMap.contains(key)) {
        m_confMap.value(key)->toString();
    }
    return fallback;
}



long Y::number(QString key, int fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        int value = m_confMap.value(key)->toInt(&ok);
        return ok ? value : fallback;
    }
    return fallback;
}


double Y::real(QString key, double fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        double value = m_confMap.value(key)->toDouble(&ok);
        return ok ? value : fallback;
    }
    return fallback;
}

QColor Y::color(QString key, QColor &fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        QVariant* variant = m_confMap.value(key);
        QColor value = variant->value<QColor>();
        return ok ? value : fallback;
    }
    return fallback;
}


QFont Y::font(QString key, QFont &fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        QVariant* variant = m_confMap.value(key);
        QFont value = variant->value<QFont>();
        return ok ? value : fallback;
    }
    return fallback;
}


QSize Y::size(QString key, QSize &fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        QSize value = m_confMap.value(key)->toSize();
        return ok ? value : fallback;
    }
    return fallback;
}

QImage Y::image(QString key, QImage &fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        QVariant* variant = m_confMap.value(key);
        QImage value = variant->value<QImage>();
        return ok ? value : fallback;
    }
    return fallback;
}


